package view.backend.product;

import exceptionManager.ExceptionManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import model.Product;
import model.handlers.ProductHandler;
import view.generaldesign.DesignLayout;

/**
 *  
 * @author Sonny Sandberg
 */
public class AdminNewProductOverview 
{
    private JPanel adminNewProductOverviewPanel;
    private AdminProduct adminProduct;
    
    private JLabel
            lblName,
            lblPurchasePrice,
            lblSalesPrice,
            lblSize,
            lblProductFamily,
            lblHasDecoration,
            lblServiceProduct;
    
    private JButton 
            btnConfirm,
            btnDeactivate,
            btnDelete;

    public AdminNewProductOverview(AdminProduct view)
    {
        adminProduct = view;
        
        adminNewProductOverviewPanel = new JPanel(null);
        adminNewProductOverviewPanel.setBounds(460, 0, 440, 255);
        adminNewProductOverviewPanel.setBorder(BorderFactory.createTitledBorder(DesignLayout.T_FIELD_RED, "Ny Vare Oversigt", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, DesignLayout.STANDARD));
        adminNewProductOverviewPanel.setOpaque(false);
        
        buildOverviewPanel();
        updateLabelsFromModel();
        
        adminNewProductOverviewPanel.setVisible(false);
    }
    
    private void buildOverviewPanel()
    {
        lblProductFamily = new JLabel("<html><b>Varegruppe:</b> </html>");
        lblProductFamily.setBounds(10, 25, 460, 18);
        lblProductFamily.setFont(DesignLayout.STANDARD);
        
        lblName = new JLabel("<html><b>Navn:</b> </html>");
        lblName.setBounds(10, 45, 460, 18);
        lblName.setFont(DesignLayout.STANDARD);
        
        lblPurchasePrice = new JLabel("<html><b>Købspris:</b> </html>");
        lblPurchasePrice.setBounds(10, 65, 460, 18);
        lblPurchasePrice.setFont(DesignLayout.STANDARD);
        
        lblSalesPrice = new JLabel("<html><b>Salgspris:</b> </html>");
        lblSalesPrice.setBounds(10, 85, 460, 18);
        lblSalesPrice.setFont(DesignLayout.STANDARD);
        
        lblSize = new JLabel("<html><b>Størrelse:</b> </html>");
        lblSize.setBounds(10, 105, 460, 18);
        lblSize.setFont(DesignLayout.STANDARD);
        
        lblHasDecoration = new JLabel("<html><b>Har Dekoration:</b> </html>");
        lblHasDecoration.setBounds(10, 125, 460, 18);
        lblHasDecoration.setFont(DesignLayout.STANDARD);
        
        lblServiceProduct = new JLabel("<html><b>Serviceprodukt:</b> </html>");
        lblServiceProduct.setBounds(10, 145, 460, 18);
        lblServiceProduct.setFont(DesignLayout.STANDARD);
        
        btnConfirm = new JButton("");
        btnConfirm.setBounds(310, 220, 120, 25);
        btnConfirm.setFont(DesignLayout.STANDARD);
        btnConfirm.setBackground(DesignLayout.T_FIELD_BG_COLOR);
        btnConfirm.setBorder(DesignLayout.T_FIELD_RED);
        btnConfirm.setEnabled(false);
        btnConfirm.addActionListener(new ActionListener() 
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                String qText = "";
                if (adminProduct.getProductModel().getId() > 0)
                {
                    qText = "Ønsker du at redigere varen?";
                }
                else
                {
                    qText = "Ønsker du oprette varen?";
                }

                int n = JOptionPane.showConfirmDialog(
                null,
                qText,
                "Bekræft Venligst",
                JOptionPane.YES_NO_OPTION);
                if (n == 0)
                {
                    try
                    {
                        int added = control.ProductControl.addAlterProduct(adminProduct.getProductModel());
                        if (added > 0)
                        {
                            adminProduct.reset();
                            adminProduct.getPrdList().buildExistingScrollPanel("", "ORDER BY productID DESC", "LIMIT 10");
                            updateLabelsFromModel();
                            adminNewProductOverviewPanel.setVisible(false);
                            adminProduct.setBtnGoToVisible(false);
                        }
                    } 
                    catch (SQLException ex)
                    {
                        ExceptionManager.handleSQLException(ex);
                    } 
                    catch (ClassNotFoundException ex)
                    {
                        ExceptionManager.handleClassNotFoundException(ex);
                    }
                }
            }
        });
        
        btnDeactivate = new JButton("");
        btnDeactivate.setBounds(180, 220, 120, 25);
        btnDeactivate.setFont(DesignLayout.STANDARD);
        btnDeactivate.setVisible(false);
        btnDeactivate.setBackground(DesignLayout.T_FIELD_BG_COLOR);
        btnDeactivate.setBorder(DesignLayout.T_FIELD_RED);
        btnDeactivate.addActionListener(new ActionListener() 
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                String qText = "";
                if (adminProduct.getProductModel().isActive())
                {
                    qText = "Ønsker du at deaktivere varen?";
                }
                else
                {
                    qText = "Ønsker du aktivere varen?";
                }

                int n = JOptionPane.showConfirmDialog(
                null,
                qText,
                "Bekræft Venligst",
                JOptionPane.YES_NO_OPTION);
                if (n == 0)
                {                 
                    try
                    {
                        boolean currentActiveState = adminProduct.getProductModel().isActive();
                        if (currentActiveState == true) 
                        { 
                            adminProduct.getProductModel().setActive(false); 
                        }
                        else
                        {
                            adminProduct.getProductModel().setActive(true);
                        }

                        int changed = control.ProductControl.addAlterProduct(adminProduct.getProductModel());
                        if (changed > 0)
                        {
                            adminProduct.getPrdList().buildExistingScrollPanel("", "ORDER BY productID DESC", "LIMIT 10");
                            updateLabelsFromModel();
                        }
                    } 
                    catch (SQLException ex)
                    {
                        ExceptionManager.handleSQLException(ex);
                    } 
                    catch (ClassNotFoundException ex)
                    {
                        ExceptionManager.handleClassNotFoundException(ex);
                    }
                }
            }
        });
        
        btnDelete = new JButton("slet");
        btnDelete.setBounds(50, 220, 120, 25);
        btnDelete.setFont(DesignLayout.STANDARD);
        btnDelete.setVisible(false);
        btnDelete.setBackground(DesignLayout.T_FIELD_BG_COLOR);
        btnDelete.setBorder(DesignLayout.T_FIELD_RED);
        btnDelete.addActionListener(new ActionListener() 
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                int n = JOptionPane.showConfirmDialog(
                null,
                "Ønsker du at slette produktet? Dette er ikke muligt at omgøre, når det først er sket.",
                "Bekræft Venligst",
                JOptionPane.YES_NO_OPTION);
                if (n == 0)
                {
                    try
                    {
                        int deleted = control.ProductControl.deleteProduct(adminProduct.getProductModel());
                        if (deleted > 0)
                        {
                            adminProduct.reset();
                            adminProduct.getPrdList().buildExistingScrollPanel("", "ORDER BY productID DESC", "LIMIT 10");
                            updateLabelsFromModel();
                            adminNewProductOverviewPanel.setVisible(false);
                            adminProduct.setBtnGoToVisible(false);
                        }
                    } 
                    catch (SQLException ex)
                    {
                        ExceptionManager.handleSQLException(ex);
                    } 
                    catch (ClassNotFoundException ex)
                    {
                        ExceptionManager.handleClassNotFoundException(ex);
                    }                   
                }
            }
        });
        
        adminNewProductOverviewPanel.add(lblProductFamily);
        adminNewProductOverviewPanel.add(lblName);
        adminNewProductOverviewPanel.add(lblPurchasePrice);
        adminNewProductOverviewPanel.add(lblSalesPrice);
        adminNewProductOverviewPanel.add(lblSize);
        adminNewProductOverviewPanel.add(lblHasDecoration);
        adminNewProductOverviewPanel.add(lblServiceProduct);
        adminNewProductOverviewPanel.add(btnConfirm);
        adminNewProductOverviewPanel.add(btnDeactivate);
        
        adminNewProductOverviewPanel.add(btnDelete);
    }
    
    public void updateLabelsFromModel()
    {
        Product model = adminProduct.getProductModel();
        
        lblProductFamily.setText("<html><b>Varegruppe:</b> "+model.getProductFamily().getId()+" ("+model.getProductFamily().getName()+")</html>");
        lblName.setText("<html><b>Navn:</b> "+model.getName()+"</html>");
        lblPurchasePrice.setText("<html><b>Købspris:</b> "+model.getPurchasePrice()+"</html>");
        lblSalesPrice.setText("<html><b>Salgspris:</b> "+model.getSalesPrice()+"</html>");
        lblSize.setText("<html><b>Størrelse:</b> "+model.getSize()+"</html>");
        lblHasDecoration.setText("<html><b>Har Dekoration:</b> "+utility.Converter.booleanToString(model.isHasDecoration())+"</html>");
        lblServiceProduct.setText("<html><b>Serviceprodukt:</b> "+utility.Converter.booleanToString(model.isRemainAfterSale())+"</html>");
        
        if (model.getId() > 0)
        {
            btnConfirm.setText("ret vare");
            btnDeactivate.setVisible(true);
            btnDelete.setVisible(true);
        }
        else
        {
            btnConfirm.setText("opret vare");
            btnDeactivate.setVisible(false);
            btnDelete.setVisible(false);
        }
        
        if (model.isCompleted() && model.getProductFamily().isCompleted())
        {
            btnConfirm.setEnabled(true);
        }
        else
        {
            btnConfirm.setEnabled(false);
        }
        
        if (model.isActive())
        {
            btnDeactivate.setText("deaktiver");
        }
        else
        {
            btnDeactivate.setText("aktiver");
        }
        if (adminProduct.getProductModel().getId() > 0)
        {
            try
            {
                Product currentProduct = ProductHandler.getProductByID(adminProduct.getProductModel().getId());
                if (currentProduct.isSold() == false && currentProduct.isRemainAfterSale() == false)
                {
                    btnDelete.setEnabled(true);
                }
                else
                {
                    btnDelete.setEnabled(false);
                }
            } 
            catch (SQLException ex)
            {
                ExceptionManager.handleSQLException(ex);
            } 
            catch (ClassNotFoundException ex)
            {
                ExceptionManager.handleClassNotFoundException(ex);
            }
        }
    }

    public JPanel getPanel()
    {
        return adminNewProductOverviewPanel;
    }
}